﻿//Copyright (c) 2010 Christopher E. S. King (http://chriskingconsulting.com)
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 
// (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, 
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do 
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using VMware.Vim;

using King.Text;

namespace King.Automation.VMWare {

    public class Program {
        public static int Main(string[] args) {
            return ConsoleApplication.EntryPoint(args);
        }
    }

    public abstract class VmWareCommand : Command {
    }

    /// <summary>
    /// Create a new VM on a VMWare server. 
    /// </summary>
    [Command]
    public class New : Command {

        protected override IEnumerable<object> Execute(Text.TextNodeContext context) {
            var client = new VimClient();
            client.Connect("http://vcenter-01");

            var arguments = new TextJoin() {
                "-psc", new CommandArgument() { VmWareDir + @"Infrastructure\vSphere PowerCLI\vim.psc1" },
                "-c", "Connect-VIServer", Server + ";", 
                "echo +-=;", // a marker to tell when to start parsing output
                "$OutputEncoding = [System.Text.Encoding]::Unicode;",

                "New-VM",
                "-Name", Name,
                "-Template", new TextNodes() { 
                    "(Get-Template '", Template, "')" 
                },

                "-VMHost", Host,
                "-OSCustomizationSpec", new TextNodes() { 
                    "(Get-OSCustomizationSpec '", Customization, "')" 
                }
            };

            yield return new TextLine() { new TextJoin() { PowerShellExe, arguments } };

            var process = new AutomationProcess(PowerShellExe, arguments).ToTextReader();

            string line = null;
            while ((line = process.ReadLine()) != null) {
                yield return line;
            }
        }

        /// <summary>
        /// The NetBIOS name of the machine.
        /// </summary>
        [CommandSwitch(1, IsRequired = true)]
        public string Name { get; set; }

        /// <summary>
        /// The template (aka image) to use to build the box. (e.g. 'Win2k8 R2 Std')
        /// </summary>
        [CommandSwitch(2, IsRequired = true)]
        public string Template { get; set; }

        /// <summary>
        /// The VMWare OSCustomizationSpec to use to attch the machine to the domain.
        /// </summary>
        [CommandSwitch(3, IsRequired = true)]
        public string Customization { get; set; }

        /// <summary>
        /// The host machine for the new VM.
        /// </summary>
        [CommandSwitch(4, IsRequired = true)]
        public string Host { get; set; }

        /// <summary>
        /// The VMWare server.
        /// </summary>
        [CommandSwitch(5, IsRequired = true)]
        public string Server { get; set; }

        /// <summary>
        /// Path to powershell.exe.
        /// </summary>
        [CommandSwitch(6, IsExistingDirectoryOrFile = true, DefaultValue=@"c:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe")]
        public string PowerShellExe { get; set; }

        /// <summary>
        /// Path to VMWare directory.
        /// </summary>
        [CommandSwitch(7, IsExistingDirectory = true, DefaultValue = @"C:\Program Files\VMware\")]
        public string VmWareDir { get; set; }
    }
}
